/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Core;
using NUnit.Framework;
using System.IO;

namespace Madason.TemplateCompiler.Tests
{
	[TestFixture]
	public class TestParser
	{
		[Test]
		public void TestComment()
		{
			string text = "b4 <!-- this is gonna disapear --> b5";
			Parser parser = new Parser(new StringReader(text));
			Token token = parser.ReadToken();
			Assert.IsNotNull(token);
			Assert.AreEqual("b4 ", token.Content);
			Assert.AreEqual(TokenType.Text, token.TokenType);
			token = parser.ReadToken();
			Assert.IsNotNull(token);
			Assert.AreEqual(" b5", token.Content);
			Assert.AreEqual(TokenType.Text, token.TokenType);
			token = parser.ReadToken();
			Assert.AreEqual(TokenType.EOF, token.TokenType);
		}

		[Test]
		public void TestScriptlet()
		{
			string text = "b4 <% int i = 1; %> b5";
			Parser parser = new Parser(new StringReader(text));
			Token token = parser.ReadToken();
			Assert.IsNotNull(token);
			Assert.AreEqual("b4 ", token.Content);
			Assert.AreEqual(TokenType.Text, token.TokenType);
			token = parser.ReadToken();
			Assert.IsNotNull(token);
			Assert.AreEqual(" int i = 1; ", token.Content);
			Assert.AreEqual(TokenType.Scriptlet, token.TokenType);						
			token = parser.ReadToken();
			Assert.IsNotNull(token);
			Assert.AreEqual(" b5", token.Content);
			Assert.AreEqual(TokenType.Text, token.TokenType);
			token = parser.ReadToken();
			Assert.AreEqual(TokenType.EOF, token.TokenType);
		}

		[Test]
		public void TestCodeblock()
		{
			string text = "b4 <script runat=\"server\"> int i = 1; </script> b5";
			Parser parser = new Parser(new StringReader(text));
			Token token = parser.ReadToken();
			Assert.IsNotNull(token);
			Assert.AreEqual("b4 ", token.Content);
			Assert.AreEqual(TokenType.Text, token.TokenType);
			token = parser.ReadToken();
			Assert.IsNotNull(token);
			Assert.AreEqual(" int i = 1; ", token.Content);
			Assert.AreEqual(TokenType.CodeBlock, token.TokenType);
			token = parser.ReadToken();
			Assert.IsNotNull(token);
			Assert.AreEqual(" b5", token.Content);
			Assert.AreEqual(TokenType.Text, token.TokenType);
			token = parser.ReadToken();
			Assert.AreEqual(TokenType.EOF, token.TokenType);
		}

		[Test]
		public void TestScriptletNotClosed()
		{
			try
			{
				string text = "b4 <% int i = 1;";
				Parser parser = new Parser(new StringReader(text));
				while (parser.ReadToken().TokenType != TokenType.EOF);
				Assert.Fail("Parser should throw Exception");
			}
			catch (ParserException e)
			{
				Assert.AreEqual("scriptlet is not closed", e.Message);
			}
		}

		[Test]
		public void TestCodeblockNotClosed()
		{
			try
			{
				string text = "b4 <script runat=\"server\"> int i = 1;";
				Parser parser = new Parser(new StringReader(text));
				while (parser.ReadToken().TokenType != TokenType.EOF) ;
				Assert.Fail("Parser should throw Exception");
			}
			catch (ParserException e)
			{
				Assert.AreEqual("codeblock is not closed", e.Message);
			}
		}
	}
}
